<?php
namespace app\common\service;

use app\index\model\UserModel;
use notification\tools\EmailNotification;
use notification\tools\WeWorkNotification;

class NoticeService{

    public $noticeChannel = [];

    private $user = [];


    public function __construct(UserModel $user)
    {
        $this->user = $user;
        if($user->email){
            $this->noticeChannel['email'] = new EmailNotification();
        }

        if($user->userId){
            $corp_id = config('wework.corp_id');
            $agent_id = config('wework.agent_id');
            $secret = config('wework.secret');

            $this->noticeChannel['userId'] = new WeWorkNotification($corp_id, $agent_id, $secret);
        }
    }

    public function sendText($title, $content) {

        $message = new \notification\message\Message($title, $content);


        foreach($this->noticeChannel as $key => $channel){
            $result = $channel->sendText($message, $this->user[$key]);
            return $result;
        }
    }
}